package com.shihan.sqlSession;

import com.shihan.config.XMLConfiguration;
import com.shihan.pojo.Configuration;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * MySqlSessionFactoryBuilder
 * 用于创建 SQLSessionFactory 对象
 */
public class MySqlSessionFactoryBuilder {

    /**
     * 用来创建 SQLSessionFactory 对象
     * @return
     */
    public SQLSessionFactory build(InputStream inputStream) throws PropertyVetoException, DocumentException {
        //1、将 InputStream 中的内容读取出来，封装到 Configuration 对象中；
        XMLConfiguration xmlConfiguration = new XMLConfiguration();
        Configuration configuration = xmlConfiguration.parseConfig(inputStream);

        //2、创建并返回一个 SQLSessionFactory 对象；
        DefaultSQLSessionFactory defaultSQLSessionFactory = new DefaultSQLSessionFactory(configuration);

        return defaultSQLSessionFactory;
    }
}
